package cn.iocoder.yudao.module.game.service.gameManufacturer;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.game.controller.admin.gameManufacturer.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.gameManufacturer.GameManufacturerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.game.dal.mysql.gameManufacturer.GameManufacturerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.game.enums.ErrorCodeConstants.*;

/**
 * 游戏厂商概况 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GameManufacturerServiceImpl implements GameManufacturerService {

    @Resource
    private GameManufacturerMapper manufacturerMapper;

    @Override
    public Long createManufacturer(GameManufacturerSaveReqVO createReqVO) {
        // 插入
        GameManufacturerDO manufacturer = BeanUtils.toBean(createReqVO, GameManufacturerDO.class);
        manufacturerMapper.insert(manufacturer);
        // 返回
        return manufacturer.getId();
    }

    @Override
    public void updateManufacturer(GameManufacturerSaveReqVO updateReqVO) {
        // 校验存在
        validateManufacturerExists(updateReqVO.getId());
        // 更新
        GameManufacturerDO updateObj = BeanUtils.toBean(updateReqVO, GameManufacturerDO.class);
        manufacturerMapper.updateById(updateObj);
    }

    @Override
    public void deleteManufacturer(Long id) {
        // 校验存在
        validateManufacturerExists(id);
        // 删除
        manufacturerMapper.deleteById(id);
    }

    private void validateManufacturerExists(Long id) {
        if (manufacturerMapper.selectById(id) == null) {
            throw exception(MANUFACTURER_NOT_EXISTS);
        }
    }

    @Override
    public GameManufacturerDO getManufacturer(Long id) {
        return manufacturerMapper.selectById(id);
    }

    @Override
    public PageResult<GameManufacturerDO> getManufacturerPage(GameManufacturerPageReqVO pageReqVO) {
        return manufacturerMapper.selectPage(pageReqVO);
    }

}